﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Interfaces;

namespace ShopB2C.SQLDataAccess
{
    public class Brand : IBrand
    {

        #region Const Variables

        private const string _brandAppend = "Brand_append";
        private const string _brandUpdate = "Brand_Update";
        private const string _brandDelete = "Brand_Delete";
        private const string _brandSelectByIds = "Brand_SelectByIds";
        private const string _brandSelect = "Brand_Select";
        private const string _brandSelectList = "Brand_SelectList";
        private const string _brandSelectIsRecomm = "Brand_SelectList_IsRecomm";
        private const string _brandBatchUpdateSequence = "Brand_BatchUpdateSequence";

        #endregion

        public Brand(string connectionString)
        {
            DbBase.ConnectionString = connectionString;
        }

        #region Brand

        public void BrandAppend(BrandEntity be)
        {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Name" , 50 , be.Name).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Int32>("@Sequence" , be.Sequence).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Url" , 250 , be.Url).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Logo" , 50 , be.Logo).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description" , 500 , be.Description).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsRecomm" , be.IsRecomm).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@ClassifyId" , be.ClassifyId).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_brandAppend , sqlParams.ToArray);
        }

        public void BrandUpdate(BrandEntity be)
        {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@BrandId" , be.BrandId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Name" , 50 , be.Name).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Int32>("@Sequence" , be.Sequence).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Url" , 250 , be.Url).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Logo" , 50 , be.Logo).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description" , 500 , be.Description).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Boolean>("@IsRecomm" , be.IsRecomm).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@ClassifyId" , be.ClassifyId).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_brandUpdate , sqlParams.ToArray);
        }

        public void BrandDelete(string brandIds)
        {
            SqlDbProcedure.ExecuteNonQuery(_brandDelete ,
                DbParams<SqlParameter>.CreateParameter<string>("@BrandIds" , brandIds));
        }

        public List<BrandEntity> GetBrandList(string brandIds)
        {
            List<BrandEntity> listResult = new List<BrandEntity>();
            SqlDataReader dr;
            if (string.IsNullOrEmpty(brandIds))
            {
                dr = SqlDbProcedure.ExecuteReader(_brandSelectByIds , null);
            }
            else
            {
                dr = SqlDbProcedure.ExecuteReader(_brandSelectByIds ,
                    DbParams<SqlParameter>.CreateParameter<string>("@BrandIds" , brandIds));
            }

            while (dr.Read())
            {
                listResult.Add(new BrandEntity()
                {
                    BrandId = (int)dr["BrandId"] ,
                    Name = dr["Name"].ToString() ,
                    Sequence = (int)dr["Sequence"] ,
                    Url = dr["Url"].ToString() ,
                    Logo = dr["Logo"].ToString() ,
                    Description = dr["Description"].ToString() ,
                    IsRecomm = (bool)dr["IsRecomm"] ,
                    ClassifyId = (int)dr["ClassifyId"]
                });
            }

            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public BrandEntity GetBrandById(int brandId)
        {
            BrandEntity be = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_brandSelect ,
                DbParams<SqlParameter>.CreateParameter<int>("@BrandId" , brandId));
            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    be = new BrandEntity()
                    {
                        BrandId = (int)dr["BrandId"] ,
                        Name = dr["Name"].ToString() ,
                        Sequence = (int)dr["Sequence"] ,
                        Url = dr["Url"].ToString() ,
                        Logo = dr["Logo"].ToString() ,
                        Description = dr["Description"].ToString() ,
                        IsRecomm = (bool)dr["IsRecomm"] ,
                        ClassifyId = (int)dr["ClassifyId"]
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return be;
        }

        public List<BrandEntity> GetBrandList(int classifyId , int pageSize , int pageIndex , out int pageCount)
        {
            pageCount = 0;
            List<BrandEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageCount" , ParameterDirection.Output , 0).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageSize" , pageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex" , pageIndex).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@ClassifyId" , classifyId).SqlDbType = SqlDbType.Int;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_brandSelectList , sqlParams.ToArray);
            if (dr.HasRows)
            {
                listResult = new List<BrandEntity>();
                while (dr.Read())
                {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new BrandEntity()
                    {
                        BrandId = (int)dr["BrandId"] ,
                        Name = dr["Name"].ToString() ,
                        Sequence = (int)dr["Sequence"] ,
                        Url = dr["Url"].ToString() ,
                        Logo = dr["Logo"].ToString() ,
                        Description = dr["Description"].ToString() ,
                        IsRecomm = (bool)dr["IsRecomm"] ,
                        ClassifyId = (int)dr["ClassifyId"] ,
                        ClassifyName = dr["ClassifyName"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<BrandEntity> GetBrandList(int classifyId , bool? isRecomm)
        {
            List<BrandEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@ClassifyId" , classifyId).SqlDbType = SqlDbType.Int;
            if (isRecomm.HasValue)
            {
                sqlParams.Add<Boolean>("@IsRecomm" , isRecomm.Value).SqlDbType = SqlDbType.Bit;
            }
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_brandSelectIsRecomm , sqlParams.ToArray);
            if (dr.HasRows)
            {
                listResult = new List<BrandEntity>();
                while (dr.Read())
                {
                    listResult.Add(new BrandEntity()
                    {
                        BrandId = (int)dr["BrandId"] ,
                        Name = dr["Name"].ToString() ,
                        Sequence = (int)dr["Sequence"] ,
                        Url = dr["Url"].ToString() ,
                        Logo = dr["Logo"].ToString() ,
                        Description = dr["Description"].ToString() ,
                        IsRecomm = (bool)dr["IsRecomm"] ,
                        ClassifyId = (int)dr["ClassifyId"] ,
                        ClassifyName = dr["ClassifyName"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }


        public void BrandBatchUpdateSequence(string brandIds , string sequences)
        {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@BrandIds" , 4000 , brandIds).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Sequences" , 4000 , sequences).SqlDbType = SqlDbType.NVarChar;
            SqlDbProcedure.ExecuteNonQuery(_brandBatchUpdateSequence , sqlParams.ToArray);
        }

        #endregion
    }
}
